package com.stone.serverless.web.interceptor;

import cn.hutool.core.util.ObjectUtil;
import com.stone.serverless.auth.domain.FilterContext;
import com.stone.serverless.auth.interceptor.StoneHandlerInterceptor;
import com.stone.serverless.auth.wrapper.StoneHttpServletRequestWrapper;
import com.stone.serverless.auth.wrapper.StoneHttpServletResponseWrapper;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description: TODO
 * @Author: MECHREVO
 * @Date: 2022/1/27 9:21
 * @Version: 1.0.0
 */
public class AuthInterceptor implements HandlerInterceptor {

    private StoneHandlerInterceptor stoneHandlerInterceptor;

    public void setStoneHandlerInterceptor(StoneHandlerInterceptor stoneHandlerInterceptor) {
        this.stoneHandlerInterceptor = stoneHandlerInterceptor;
    }

    /**
     * 每次请求之前触发的方法
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {


        if (ObjectUtil.isNull(stoneHandlerInterceptor)) {
            return true;
        }
        Boolean aBoolean = stoneHandlerInterceptor.preHandle(new FilterContext(
                new StoneHttpServletRequestWrapper(request), new StoneHttpServletResponseWrapper(response), handler));
        if (null == aBoolean) {
            return true;
        }
        return aBoolean;
    }
}
